/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bgate.game;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;

/**
 *
 * @author user
 */
public class UserInfoRMS extends BaseRMS {

    /*
     * Cac thuoc tinh trong game can phai luu tru: Game da
     * kich hoat chua, level hien tai, so tien trong game,...
     */
    public int isGameActive = 0;

    public int level = 0;

    public int money = 0;

    public UserInfoRMS(String rmsName) {
        super(rmsName);
    }

    public void loadInfo() {
        try {
            this.open();
            this.loadData();
            if (this.getRecordStore() != null) {
                this.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void saveGameActive(int isActive) {
        try {
            this.open();
            this.isGameActive = isActive;
            updateData();
            if (this.getRecordStore() != null) {
                this.close();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void saveGameLevel(int level) {
        try {
            this.open();
            this.level = level;
            updateData();
            if (this.getRecordStore() != null) {
                this.close();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void saveGameMoney(int money) {
        try {
            this.open();
            this.money = money;
            updateData();
            if (this.getRecordStore() != null) {
                this.close();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    void loadData() {
        try {
            byte[] record = this.getRecordStore().getRecord(1);
            DataInputStream istream = new DataInputStream(new ByteArrayInputStream(record, 0, record.length));
            this.isGameActive = istream.readInt();
            this.level = istream.readInt();
            this.money = istream.readInt();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    void createDefaultData() {
        try {
            ByteArrayOutputStream bstream = new ByteArrayOutputStream(28);
            DataOutputStream ostream = new DataOutputStream(bstream);
            ostream.writeInt(isGameActive);
            ostream.writeInt(level);
            ostream.writeInt(money);
            ostream.flush();
            ostream.close();
            byte[] record = bstream.toByteArray();
            this.getRecordStore().addRecord(record, 0, record.length);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    void updateData() {
        try {
            ByteArrayOutputStream bstream = new ByteArrayOutputStream(28);
            DataOutputStream ostream = new DataOutputStream(bstream);
            ostream.writeInt(isGameActive);
            ostream.writeInt(level);
            ostream.writeInt(money);
            ostream.flush();
            ostream.close();
            byte[] record = bstream.toByteArray();
            this.getRecordStore().setRecord(1, record, 0, record.length);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
